package com.oocl.o2o.admin.listener;

import java.io.IOException;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

import com.oocl.o2o.admin.entity.Advertisement;
import com.oocl.o2o.admin.entity.Merchant;
import com.oocl.o2o.admin.service.AdvertisementService;
import com.oocl.o2o.admin.service.MerchantService;
import com.oocl.o2o.admin.util.Base64Unit;
import com.oocl.o2o.admin.util.FileUtil;
import com.oocl.o2o.connectutil.assist.ConfigUtil;
import com.oocl.o2o.connectutil.assist.JsonUtil;
import com.oocl.o2o.connectutil.dto.AdvertisementDTO;
import com.oocl.o2o.connectutil.dto.MerchantDTO;

public class AdvertisementApplyJMSListener implements MessageListener{

	@Resource(name="advertisementServiceImpl")
	private AdvertisementService advertisementService;
	
	private static Logger logger = Logger.getLogger(AdvertisementApplyJMSListener.class);
	
	private static int advertCount=0;
	
	@Override
	public void onMessage(Message message) {
			
		TextMessage textMsg = (TextMessage) message; 
		
        try {  
        	advertCount++;
            logger.info("接收新的广告消息内容");
            AdvertisementDTO advertisementDTO =  JsonUtil.getObjectFromJson(textMsg.getText(), AdvertisementDTO.class);
            advertisementService.applyAdvertisement(advertisementDTO);
            message.acknowledge();
        } catch (JMSException e) {  
            e.printStackTrace();  
        }
	}

	public static int getAdvertCount() {
		return advertCount;
	}

	public static void setAdvertCount(int advertCount) {
		AdvertisementApplyJMSListener.advertCount = advertCount;
	}
}
